﻿using DocumentFormat.OpenXml.Lite.Core.Office;
using WB = DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Base;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.FooterElements;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.SectionProperties;
using WT = DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Types;
using WTx = DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Texts;
using WRE = DocumentFormat.OpenXml.Lite.WordprocessingML.Word.RunElements;
using WRP = DocumentFormat.OpenXml.Lite.WordprocessingML.Word.RunProperties;
using WPP = DocumentFormat.OpenXml.Lite.WordprocessingML.Word.ParagraphProperties;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Samples.Word
{
    class Footers
    {
        public static void Create()
        {
            WB.Document doc = DocumentManager.Instance.CreateWordDocument();
            doc.CreateBody().AddParagraph().AddRun().AddSimpleText("Docx sample with page numbers.");

            WB.Paragraph propP = doc.Body.AddParagraph();

            WB.Footer footer = doc.CreateFooter();
            propP.AddSectionProperty(new FooterReference()
            {
                Type = WT.ST_HdrFtr.Default,
                RelationshipId = footer.RelationshipId,
            });

            BLSDocumentTag docTag;
            footer.Elements.Add(docTag = new BLSDocumentTag()
            {
                Property = new BLSProperties()
                {
                    Id = new RequiredValue<int>(new Random().Next(100000)),
                    PartObject = new PartObject()
                    {
                        GalleryFilter = new RequiredValue<string>("Page Numbers (Bottom of Page)")
                    }
                },
                CharacterProperty = new BLSEndProperties(),
                Content = new BLSContent()
            });
            docTag.CharacterProperty.RunProperties.Properties.Add(new WRP.NoProof());

            WB.Paragraph p = new WB.Paragraph();
            docTag.Content.Elements.Add(p);
            p.AddProperty(new WPP.ParagraphStyle() { Value = "Footer" });
            p.AddProperty(new WPP.ParagraphAlignment() { Value = WT.ST_Jc.Center });

            p.AddRun().AddElement(new WRE.ComplexFieldCharacter() { CharType = WT.ST_FldCharType.Begin });
            p.AddRun().AddElement(new WTx.FieldCode() { Space = "preserve", Value = @" PAGE   \* MERGEFORMAT " });
            p.AddRun().AddElement(new WRE.ComplexFieldCharacter() { CharType = WT.ST_FldCharType.Separate });

            WB.Run r1 = p.AddRun();
            r1.AddProperty(new WRP.NoProof());
            r1.AddSimpleText("1");

            WB.Run r2 = p.AddRun();
            r2.AddProperty(new WRP.NoProof());
            r2.AddElement(new WRE.ComplexFieldCharacter() { CharType = WT.ST_FldCharType.End }); 
            
            doc.Body.AddParagraph().AddRun().AddElement(new WRE.LastCalculatedPageBreak());

            doc.Save(@"samples\footer.docx");

            WB.Document parsed = null;
            if (DocumentManager.Instance.OpenWordDocument(@"samples\footer.docx", out parsed))
            {
                Console.WriteLine("Sample created and parsed succesfully!");
            }
            Console.ReadLine();
        }
    }
}
